#!/bin/bash

shellout(){
 echo 
 echo "ERROR $? in $1 commande";
 echo
 exit 2;
}

run_cmd(){
    echo -n "  $1" && $1 > /dev/null 2>&1 || shellout "$1" ; echo
}

umask 022

VERSION=`tail -1 ../src/VERSION`
CURRENTUSER=`logname`
WHOAMI=`whoami`
STARTDIR=`pwd`
PDIR=/var/tmp/package-macosx-kakola-$VERSION/
BINARY=$STARTDIR/../binary/
ARCH="intel-universal-i386-x86_64"
mkdir -p "$STARTDIR/../build/"
PKGNAME=$STARTDIR/../build/kakola-$ARCH-$VERSION.pkg
DMGNAME=$STARTDIR/../build/kakola-$ARCH-$VERSION.dmg
PACKAGEMAKER="/usr/bin/packagemaker"
FAKE_ROOT="${PDIR}/root/"
SHARED="$FAKE_ROOT/Users/Shared/"
APPLICATIONS="$FAKE_ROOT/Applications/"
PROFILES="$FAKE_ROOT/Applications/kakola.app/Contents/Resources/profiles/"
FFMPEG_VERSION=`cat ../tools/lion/video_tools_scripts/ffmpeg.sh  | grep VERSION | head -n 1 | awk -F"=" '{print $2}'`
PKG_OSX_TEMPLATE=$STARTDIR/../package-macosx-kakola-templates/
PKG_OSX_TMP_TEMPLATE="${PDIR}/package-macosx-kakola-templates/"

if [ ! "$WHOAMI" = "root" ]
then
    echo "You are running this program with user $WHOAMI"
    echo "You must be root to create the package"
    echo "Try with sudo $0"
    echo
    exit 1
fi

if echo "$STARTDIR" | grep " "
then
    shellout "$STARTDIR contain space, dangerous existing, rename your folder"
fi

echo ""

URL_VIDEO_TOOLS=http://kakola.googlecode.com/files/video-tools-${FFMPEG_VERSION}-x86_64.dmg
[ -f "$STARTDIR/../build/video-tools-${FFMPEG_VERSION}-x86_64.dmg" ] && URL_VIDEO_TOOLS="$STARTDIR/../build/video-tools-${FFMPEG_VERSION}-x86_64.dmg"
[ -f "$STARTDIR/../binary/video-tools-${FFMPEG_VERSION}-x86_64.dmg" ] && URL_VIDEO_TOOLS="$STARTDIR/../build/video-tools-${FFMPEG_VERSION}-x86_64.dmg"

rm -rf "$PDIR"

cd $STARTDIR/
sudo -u $CURRENTUSER sed -e 's#appIcon.rc#icons/application-icon.icns#g' ../src/kakola.pro > ../src/kakola.mac.pro


echo
cd $STARTDIR
echo "Building kakola project ..."
CMD="sudo -u $CURRENTUSER sh ../src/build.mac.sh"
run_cmd "$CMD"

echo
cd $STARTDIR/../src/
echo "Building macdeployqt project ..."
CMD="chown -R $CURRENTUSER:admin macdeployqt"
run_cmd "$CMD"
CMD="cd  macdeployqt/macdeployqt"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER qmake -spec macx-g++ -r"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER make clean"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER make"
run_cmd "$CMD"
MACDEPLOYQT="macdeployqt/bin/macdeployqt"


echo
cd $STARTDIR/../src/
echo "Running $MACDEPLOYQT"
CMD="sudo -u $CURRENTUSER $MACDEPLOYQT kakola.app -dmg"
run_cmd "$CMD"
rm -f kakola.dmg



echo
echo -n "Creating directory ... $FAKE_ROOT"
rm -rf "$FAKE_ROOT"
CMD="mkdir -p $FAKE_ROOT"
run_cmd "$CMD"

echo
echo "Create directory $APPLICATIONS"
CMD="mkdir -p $APPLICATIONS"
run_cmd "$CMD"

CMD="rm -rf $APPLICATIONS/kakola.app"
run_cmd "$CMD"

CMD="cp -R kakola.app $APPLICATIONS"
run_cmd "$CMD"

CMD="cd $FAKE_ROOT"
run_cmd "$CMD"

CMD="chmod -R 755 $FAKE_ROOT" 
run_cmd "$CMD"

echo
echo "Copy documentations, ressources, translate, ... into package"
CMD="mkdir -p $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"
CMD="cp -R $STARTDIR/../documentations $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"
CMD="mkdir $APPLICATIONS/kakola.app/Contents/Resources/documentations/translate/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/kakola_*.qm $APPLICATIONS/kakola.app/Contents/Resources/documentations/translate/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/VERSION $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/dvdrip/dvd-extract-commande.sh $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"


CMD="mkdir -p ${FAKE_ROOT}/usr/bin/"
run_cmd "$CMD"

cp $STARTDIR/../src/{addDatePrefixToFilename.sh,copyDateInformation.sh,addDatePrefixToAllFilenameInThisDirectory.sh,find_duplicate_file_v2.pl} ${FAKE_ROOT}/usr/bin/
CMD="cp $STARTDIR/../src/{addDatePrefixToFilename.sh,copyDateInformation.sh,addDatePrefixToAllFilenameInThisDirectory.sh,find_duplicate_file_v2.pl} ${FAKE_ROOT}/usr/bin/"
#run_cmd "$CMD"
echo $CMD

echo
echo "Creating directory ..."
CMD="mkdir -p $PROFILES"
run_cmd "$CMD"
CMD="cp $STARTDIR/../profiles/mac/*.profile $PROFILES"
run_cmd "$CMD"
CMD="mkdir -p $APPLICATIONS/kakola.app/Contents/Resources/Binary/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../tools/lion/templates/Resources/background.tiff $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"


echo
echo "Changing owner and permission"
CMD="chown -R root:wheel $APPLICATIONS"
run_cmd "$CMD"
CMD="chown -R root:wheel $APPLICATIONS/kakola.app"
run_cmd "$CMD"
CMD="chmod -R 775 $APPLICATIONS/kakola.app"
run_cmd "$CMD"

CMD="cp -a $PKG_OSX_TEMPLATE/ $PKG_OSX_TMP_TEMPLATE/"
run_cmd "$CMD"

sed -e 's/VERSION/$VERSION/g' $PKG_OSX_TEMPLATE/Description.plist > $PKG_OSX_TMP_TEMPLATE/Description.plist
cp $PKG_OSX_TMP_TEMPLATE/Description.plist $PKG_OSX_TMP_TEMPLATE/resources/Description.plist
cp $PKG_OSX_TMP_TEMPLATE/Description.plist $PKG_OSX_TMP_TEMPLATE/resources/French.lproj/
cp $PKG_OSX_TMP_TEMPLATE/resources/*.rtf $PKG_OSX_TMP_TEMPLATE/resources/French.lproj/

rm -rf $PKGNAME
echo;
echo "Creating package $PKGNAME"
CMD="$PACKAGEMAKER -build -p $PKGNAME -f $FAKE_ROOT -ds  \
 -r $PKG_OSX_TMP_TEMPLATE/resources/ \
 -s $PKG_OSX_TMP_TEMPLATE/resources/ \
 -i $PKG_OSX_TMP_TEMPLATE/Info.plist \
 -d $PKG_OSX_TMP_TEMPLATE/Description.plist"
run_cmd "$CMD"

echo
echo

CMD="defaults delete $PKG_OSX_TMP_TEMPLATE/Info.plist IFPkgPathMappings"
echo "  $CMD" && $CMD > /dev/null 2>&1 
CMD="cp $PKG_OSX_TMP_TEMPLATE/Info.plist $PKGNAME/Contents/Info.plist"
run_cmd "$CMD"
CMD="cp $PKG_OSX_TMP_TEMPLATE/resources/background.tiff $PKGNAME/Contents/Resources/"
run_cmd "$CMD"
CMD="rm -f $PKGNAME/Contents/Resources/TokenDefinitions.plist"
run_cmd "$CMD"

rm -rf "$STARTDIR/../build/kakola-i386-$VERSION.dmg"
CMD="hdiutil create $DMGNAME -srcfolder $PKGNAME -ov"
run_cmd "$CMD"


echo
echo
echo "Files bellow have been generated :"
echo
echo
du -sh "$DMGNAME"
du -sh "$PKGNAME"
echo
echo
